<template>
  <div class="create">
    <el-form  :model="formCreate" class="demo-form-inline">
      <el-form-item label="标题：">
        <el-input v-model.trim="formCreate.title" placeholder="title"></el-input>
      </el-form-item>
      <el-form-item label="内容">
   
        <el-input type="textarea" v-model.trim="formCreate.content" placeholder="content"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { createPost } from "../api/list";
export default {
  name: "create",
  data() {
    return {
      formCreate: {
        title: "",
        content: "",
      }
    };
  },
  computed:{
    name(){
      return this.$store.state.userName
      }
  },
  methods: {
    onSubmit() {
      if (this.formCreate.title === "" || this.formCreate.content === "") {
        this.$message.warning("不能为空");
      } else {
        console.log(this.$store.state)
      this.formCreate.name = name;
        createPost(this.formCreate)
          .then(res => {
            if (res.data.code === 200) {
              this.$message.success("发表成功");
              window.location.href = "/";
            } else {
              this.$message.error(res.data.message);
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    }
  }
};
</script>